package com.durian.base.frame.dialog;

import android.app.Dialog;
import android.content.Context;
import androidx.fragment.app.FragmentManager;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import com.durian.base.R;


public class CustomProgressDialog extends Dialog implements ILoadingDialog{
    private Context context;

    public CustomProgressDialog(Context context) {
        this(context, R.style.base_custom_dialog_style);
        this.context = context;
        init(context);
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
        init(context);
    }

    private CustomProgressDialog init(Context context) {
        setContentView(R.layout.base_custom_progress_dialog);
        getWindow().getAttributes().gravity = Gravity.CENTER;
        setCanceledOnTouchOutside(false);
        return this;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        ImageView spaceshipImage = findViewById(R.id.img);
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                context, R.anim.base_cutom_progress_anim);
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);

        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            //调节灰色背景透明度[0-1]，默认0.5f
            lp.dimAmount = 0.1f;
            window.setAttributes(lp);
        }
    }

    @Override
    public ILoadingDialog setCancel(boolean isCancel) {
        setCancelable(isCancel);
        setCanceledOnTouchOutside(isCancel);
        return this;
    }

    @Override
    public ILoadingDialog setText(String text) {
        return this;
    }

    @Override
    public ILoadingDialog showDialog(FragmentManager manager) {
        if(this.getOwnerActivity()!=null&&!this.getOwnerActivity().isFinishing())
            show();
        return this;
    }

}
